<?php

    /**
     * This file is part of the Intelligencer package.
     * For the full copyright and license, please view the LICENSE
     * file that was distributed with this source code.
     * 
     * @author      drapeko <roman.drapeko@gmail.com>
     * @copyright   Roman Drapeko <roman.drapeko@gmail.com>, 2009 - present
     */

    /**
     * ClassRules are persistent cacheable rules. ClassRules contain a list of classes and their locations.
     * They are very straigtforward and are used by Intelligencer for caching the results of Simple and Complex rules. 
     * 
     * @author drapeko
     */
    class itClassRules extends itRules implements itPersistentCacheableI {
        protected $rules = array();
        protected $basePath = '';
        
        /**
         * Adds new class
         * @param string $className
         * @param string $path
         * @return itClassRules itself
         */
        public function addClass($className, $path) {
            list($namespace, $class) = itUtil::getLowerClassAndNS($className);
            $this->rules[$namespace][$class] = $path;
            return $this;
        }
        
        /**
         * Returns the location by class name. Null is returned if the class has not been set. 
         * Prefix will be added to the location if it has been set.
         * @param string $className
         * @return string $path
         * @see setPrefix
         */
        public function getClass($className) {
            list($namespace, $class) = itUtil::getLowerClassAndNS($className);
            
            if ($this->hasClass($className)) {
                return $this->getPrefix().$this->rules[$namespace][$class];
            }
            return null;
        }
        
        /**
         * Checks whether Rules have the particular class.
         * @param string $className
         * @return boolean
         */
        public function hasClass($className) {
            list($namespace, $class) = itUtil::getLowerClassAndNS($className);
            if (array_key_exists($namespace, $this->rules)) {
                if (array_key_exists($class, $this->rules[$namespace])) {
                    return true;
                }
            }
            return false;
        }
        
        /**
         * (non-PHPdoc)
         * @see Intelligencer/LibUtil/Cache/itPersistentCacheableI#toPersistentCache()
         */
        public function toPersistentCache() {
            return $this->rules;
        }
        
        /**
         * (non-PHPdoc)
         * @see Intelligencer/LibUtil/Cache/itPersistentCacheableI#fromPersistentCache($val)
         */
        public function fromPersistentCache($rules) {
            $this->rules = $rules;
        }
        
        /**
         * Returns an array representation of the rules
         * @return array
         */
        public function toArray() {
            return $this->rules;
        }
        
        /**
         * Sets the prefix that will be added to all locations when you call getClass()
         * @param $basePath
         * @return itClassRules itself
         */
        public function setPrefix($basePath) {
            $this->basePath = $basePath;
            return $this;
        }
        
        /**
         * Returns the prefix
         * @return string
         */
        public function getPrefix() {
            return $this->basePath;
        }
        
    }
    
?>